package com.alks.common.utils.excelUtils.fluExcel;

import cn.hutool.core.io.resource.ClassPathResource;
import com.alks.entity.exception.ServiceErrorException;
import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlEngine;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReader;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;
import org.jxls.util.TransformerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static org.reflections.Reflections.log;

public class JxlsExcelUtil {
    private static String XML_DEFINE_PATH = "/templates/import/";
    private static String EXCEL_MODAL_PATH = "/jxls/modal/";

    public JxlsExcelUtil() {
    }

    public static void exportExcel(String modalName, String destFileName, Map<String, Object> data,HttpServletResponse response) {
         exportExcel(modalName, destFileName, data,response, new HashMap());
    }

    public static void exportExcel(String modalName, String destFileName, Map<String, Object> data, HttpServletResponse response, Map<String, Object> funcs) {
        try {
            destFileName = destFileName.replaceAll("&", "-");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + destFileName+"xls");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            InputStream fins = new ClassPathResource(modalName).getStream();
            Throwable var7 = null;
            try {
                OutputStream fouts = response.getOutputStream();
                Throwable var9 = null;
                try {
                    JxlsHelper jxlsHelper = JxlsHelper.getInstance();
                    Context context = new Context();
                    Iterator var12 = data.keySet().iterator();

                    while(var12.hasNext()) {
                        String key = (String)var12.next();
                        context.putVar(key, data.get(key));
                    }
                    Transformer trans = TransformerFactory.createTransformer(fins, fouts);
                    JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)trans.getTransformationConfig().getExpressionEvaluator();
                    JexlBuilder jb = new JexlBuilder();
                    funcs.put("utils", new JxlsUtils());
                    jb.namespaces(funcs);
                    JexlEngine je = jb.create();
                    evaluator.setJexlEngine(je);
                    jxlsHelper.setUseFastFormulaProcessor(false).processTemplate(context, trans);
                } catch (Throwable var39) {
                    var9 = var39;
                    throw var39;
                } finally {
                    if (fouts != null) {
                        if (var9 != null) {
                            try {
                                fouts.close();
                            } catch (Throwable var38) {
                                var9.addSuppressed(var38);
                            }
                        } else {
                            fouts.close();
                        }
                    }

                }
            } catch (Throwable var41) {
                var7 = var41;
                throw var41;
            } finally {
                if (fins != null) {
                    if (var7 != null) {
                        try {
                            fins.close();
                        } catch (Throwable var37) {
                            var7.addSuppressed(var37);
                        }
                    } else {
                        fins.close();
                    }
                }

            }
        } catch (Exception var43) {
            var43.printStackTrace();
            throw new ServiceErrorException("导出文件失败！");
        }
    }

    public static void readExcel(MultipartFile file, String defineXmlName, Map<String, Object> data) throws Exception {
        InputStream inputXML = JxlsExcelUtil.class.getResourceAsStream(XML_DEFINE_PATH + defineXmlName + ".xml");
        Throwable var4 = null;

        try {
            BufferedInputStream inputXLS = new BufferedInputStream(file.getInputStream());
            Throwable var6 = null;

            try {
                XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
                mainReader.read(inputXLS, data);
            } catch (Throwable var29) {
                var6 = var29;
                throw var29;
            } finally {
                if (inputXLS != null) {
                    if (var6 != null) {
                        try {
                            inputXLS.close();
                        } catch (Throwable var28) {
                            var6.addSuppressed(var28);
                        }
                    } else {
                        inputXLS.close();
                    }
                }

            }
        } catch (Throwable var31) {
            var4 = var31;
            throw var31;
        } finally {
            if (inputXML != null) {
                if (var4 != null) {
                    try {
                        inputXML.close();
                    } catch (Throwable var27) {
                        var4.addSuppressed(var27);
                    }
                } else {
                    inputXML.close();
                }
            }

        }

    }
}
